﻿using System;
using System.Web.Configuration;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class CachingCacheSectionManager : ConfigurationManagerBase
	{
		private CacheSection section;

		public CachingCacheSectionManager()
		{
			this.section = (CacheSection)base.GetConfigurationSection("system.web/caching/cache");
		}

		public void Modify(bool disableMemoryCollection, bool disableExpiration, long privateBytesLimit,
			int percentagePhysicalMemoryUsedLimit, TimeSpan privateBytesPollTime)
		{
			this.section.DisableMemoryCollection = disableMemoryCollection;
			this.section.DisableExpiration = disableExpiration;
			this.section.PrivateBytesLimit = privateBytesLimit;
			this.section.PercentagePhysicalMemoryUsedLimit = percentagePhysicalMemoryUsedLimit;
			this.section.PrivateBytesPollTime = privateBytesPollTime;
			this.Save();
		}
		public CacheSection GetCacheSection()
		{
			return this.section;
		}
	}

	public sealed class CachingOutputCacheSectionManager : ConfigurationManagerBase
	{
		private OutputCacheSection section;

		public CachingOutputCacheSectionManager()
		{
			this.section = (OutputCacheSection)base.GetConfigurationSection("system.web/caching/outputCache");
		}

		public void Modify(bool enableOutputCache, bool enableFragmentCache,
			bool sendCacheControlHeader, bool omitVaryStar
)
		{
			this.section.EnableOutputCache = enableOutputCache;
			this.section.EnableFragmentCache = enableFragmentCache;
			this.section.SendCacheControlHeader = sendCacheControlHeader;
			this.section.OmitVaryStar = omitVaryStar;
			this.Save();
		}
		public OutputCacheSection GetOutputCacheSection()
		{
			return this.section;
		}
	}

	public sealed class CachingOutputCacheSettingsSectionManager : ConfigurationManagerBase
	{
		private OutputCacheSettingsSection section;

		public CachingOutputCacheSettingsSectionManager()
		{
			this.section = (OutputCacheSettingsSection)base.GetConfigurationSection("system.web/caching/outputCacheSettings");
		}

		public void AddOutputCacheProfile(string name, bool enabled, int duration, System.Web.UI.OutputCacheLocation location,
			string sqlDependency, string varyByCustom, string varyByControl, string varyByHeader, string varyByParam, bool noStore)
		{
			OutputCacheProfile profile = new OutputCacheProfile(name);
			profile.Enabled = enabled;
			profile.Duration = duration;
			profile.Location = location;
			profile.SqlDependency = sqlDependency;
			profile.VaryByCustom = varyByCustom;
			profile.VaryByControl = varyByControl;
			profile.VaryByHeader = varyByHeader;
			profile.VaryByParam = varyByParam;
			profile.NoStore = noStore;

			this.section.OutputCacheProfiles.Add(profile);
			this.Save();
		}
		public void ModifyOutputCacheProfile(string name, bool enabled, int duration, System.Web.UI.OutputCacheLocation location,
			string sqlDependency, string varyByCustom, string varyByControl, string varyByHeader, string varyByParam, bool noStore)
		{
			this.section.OutputCacheProfiles[name].Enabled = enabled;
			this.section.OutputCacheProfiles[name].Duration = duration;
			this.section.OutputCacheProfiles[name].Location = location;
			this.section.OutputCacheProfiles[name].SqlDependency = sqlDependency;
			this.section.OutputCacheProfiles[name].VaryByCustom = varyByCustom;
			this.section.OutputCacheProfiles[name].VaryByControl = varyByControl;
			this.section.OutputCacheProfiles[name].VaryByHeader = varyByHeader;
			this.section.OutputCacheProfiles[name].VaryByParam = varyByParam;
			this.section.OutputCacheProfiles[name].NoStore = noStore;
			this.Save();
		}
		public void RemoveOutputCacheProfile(string name)
		{
			this.section.OutputCacheProfiles.Remove(name);
			this.Save();
		}
		public void ClearOutputCacheProfiles()
		{
			this.section.OutputCacheProfiles.Clear();
			this.Save();
		}
		public OutputCacheProfile GetOutputCacheProfile(string name)
		{
			return this.section.OutputCacheProfiles[name];
		}
		public OutputCacheProfileCollection GetOutputCacheProfileCollection()
		{
			return this.section.OutputCacheProfiles != null ? this.section.OutputCacheProfiles : new OutputCacheProfileCollection();
		}
	}

	public sealed class CachingSqlCacheDependencySectionManager : ConfigurationManagerBase
	{
		private SqlCacheDependencySection section;

		public CachingSqlCacheDependencySectionManager()
		{
			this.section = (SqlCacheDependencySection)base.GetConfigurationSection("system.web/caching/sqlCacheDependency");
		}

		public void Modify(bool enabled, int pollTime)
		{
			this.section.Enabled = enabled;
			this.section.PollTime = pollTime;
			this.Save();
		}
		public SqlCacheDependencySection GetSqlCacheDependencySection()
		{
			return this.section;
		}

		public void AddDataBase(string name, string connectionStringName, int pollTime)
		{
			SqlCacheDependencyDatabase dataBase = new SqlCacheDependencyDatabase(
				name, connectionStringName, pollTime);

			this.section.Databases.Add(dataBase);
			this.Save();
		}
		public void ModifyDataBase(string name, string connectionStringName, int pollTime)
		{
			this.section.Databases[name].ConnectionStringName = connectionStringName;
			this.section.Databases[name].PollTime = pollTime;
			this.Save();
		}
		public void RemoveDataBase(string name)
		{
			this.section.Databases.Remove(name);
			this.Save();
		}
		public void ClearDataBases()
		{
			this.section.Databases.Clear();
			this.Save();
		}
		public SqlCacheDependencyDatabase GetSqlCacheDependencyDatabase(string name)
		{
			return this.section.Databases[name];
		}
		public SqlCacheDependencyDatabaseCollection GetSqlCacheDependencyDatabaseCollection()
		{
			return this.section.Databases != null ? this.section.Databases : new SqlCacheDependencyDatabaseCollection();
		}
	}
}
